﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WarehouseWebAPP.code.BLL;

namespace WarehouseWebAPP.Pages.user.Edit_Entities
{
    public partial class EditItem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ErrorLabel.Visible = false;
        }

       

        protected void ItemDetailsView_DataBound(object sender, EventArgs e)
        {

            Label vendor = (Label)ItemDetailsView.FindControl("VendorLabel");
            Label supplier = (Label)ItemDetailsView.FindControl("SupplierLabel");

            if (vendor != null && supplier != null)
            {
                VendorBLL vendorBLL = new VendorBLL();
                SupplierBLL supplierBLL = new SupplierBLL();
                try
                {
                    vendor.Text = vendorBLL.VendorGetByNum(new Guid(vendor.Text))[0].vendor_name;
                    supplier.Text = supplierBLL.SupplierGetByNum(new Guid(supplier.Text))[0].supplier_name;
                }
                finally
                {
                    //TODO
                    //we should add dispose here
                    // we need to add dispose function foreach bll
                    // or maybe the detailsview disposes alone ,
                    // need to check, because there exists disposing event

                }

            }
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            LendItemBLL BLL = new LendItemBLL();
            String Previous = BLL.LendItemByNum((Guid)ItemDetailsView.SelectedValue)[0].lend_item_serial;


            if (Previous != args.Value)
            {
                try
                {
                    if (BLL.LendItemBySerial(args.Value).Rows.Count > 0)
                        args.IsValid = false;
                    else
                        args.IsValid = true;
                }
                finally
                {
                    //TODO: Dispose
                }
            }

        }
        protected void ItemDetailsView_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
        {
            try
            {
                Label Label = (Label)ItemDetailsView.FindControl("ItemLabel");
                LendItemBLL BLL = new LendItemBLL();
                Guid guid = BLL.LendItemBySerial(Label.Text)[0].lend_item_num;

                LendBLL CBLL = new LendBLL();
                if (CBLL.LendGetByItem(guid).Rows.Count > 0)
                {
                    e.Cancel = true;
                    ErrorLabel.Visible = true;
                }
            }
            finally
            {
                //TODO: Dispose
            }
        }



        protected void ItemDetailsView_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            MailPanel1.Visible = false;
            ErrorLabel.Visible = false;
            SentLabel.Visible = false;
            if (e.CommandName == "MailItem")
            {

                CustomerRadioButton.Enabled = true;
                SupplierRadioButton.Enabled = true;
                CustomerComboBox.Enabled = true;
                SentLabel.Visible = false;
                NotesTextBox.Text = "";
                NotesTextBox.Enabled = true;
                SendButton.Enabled = true;
                if (MailPanel1.Visible == false)
                {
                    MailPanel1.Visible = true;
                    SendButton.Focus();
                }
                else MailPanel1.Visible = false;
                ItemDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
            }

        }

        protected void SendButton_Click(object sender, EventArgs e)
        {
            try
            {
                SentLabel.Focus();
                LendItemBLL BLL = new LendItemBLL();
                VendorBLL VBLL = new VendorBLL();
                SupplierBLL SBLL = new SupplierBLL();
                string EmailAddress;
                string Subject = "Entity Details have been sent to you by The WareHouse";
                Guid Key = new Guid(ItemDetailsView.DataKey.Value.ToString());
                String ItemSerial = BLL.LendItemByNum(Key)[0].lend_item_serial;
                String ItemDesc = BLL.LendItemByNum(Key)[0].lend_item_description;
                String ItemVendor = VBLL.VendorGetByNum(BLL.LendItemByNum(Key)[0].lend_item_vendor)[0].vendor_name;
                String ItemSupplier = SBLL.SupplierGetByNum(BLL.LendItemByNum(Key)[0].lend_item_supplier)[0].supplier_name;
                String IsAvailable = BLL.LendItemByNum(Key)[0].lend_item_available.ToString();

                String[] fields = { "Item Serial", "Item Description", "Item Vendor", "Item Supplier", "Item Available" };
                String[] values = { ItemSerial, ItemDesc, ItemVendor, ItemSupplier, IsAvailable };

                String EntityString = Mail.HTMLEntityDecorator("Item Details", fields, values);
                if (SupplierRadioButton.Checked)
                {
                    string SupplierMail = SBLL.SupplierGetByNum(BLL.LendItemByNum(Key)[0].lend_item_supplier)[0].supplier_email;
                    EmailAddress = SupplierMail;

                }
                else
                {
                    CustomerBLL CBLL = new CustomerBLL();
                    string CustomerMail = CBLL.CustomerGetByNum(new Guid(CustomerComboBox.SelectedValue))[0].customer_email;

                    EmailAddress = CustomerMail;
                }

                Boolean res = Mail.SendMail(EmailAddress, Subject, Mail.SenderNotes(NotesTextBox.Text) + EntityString);
                if (res == false)
                {
                    SentLabel.Text = "Mail Failed";
                }
            }

            catch (Exception)
            {
                SentLabel.Text = "Mail Failed";

            }

            finally
            {
                //TODO:Dispose;
                CustomerRadioButton.Enabled = false;
                SupplierRadioButton.Enabled = false;
                NotesTextBox.Enabled = false;
                SendButton.Enabled = false;
                CustomerComboBox.Enabled = false;
                SentLabel.Visible = true;
                MailPanel1.Visible = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SentLabel.Visible = false;
            MailPanel1.Visible = false;
        }




    }
}
